package com.ldy.seal.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.seal.entity.UserCert;
import com.ldy.seal.mapper.UserCertMapper;
import com.ldy.seal.service.UserCertService;

@Service
public class UserCertServiceImpl implements UserCertService{

	@Autowired
	private UserCertMapper mapper;
	
	@Override
	public Page<UserCert> selectPage(int current, int size, String searchText) {
		Page<UserCert> page = new  Page<UserCert>(current, size);
		page.setRecords(mapper.selectUserPage(page,searchText));
		return page;
	}

	@Override
	public UserCert selectByUserId(String userId) {
		QueryWrapper<UserCert> query = new QueryWrapper<>();
		query.eq("user_id", userId);
		UserCert cert = mapper.selectOne(query);
		return cert;
	}

	@Override
	public void saveOrUpdate(UserCert user) {
		UserCert cert = this.selectByUserId(user.getUserId());
		Date now = new Date();
		if (null == cert) {
			// 不存在，新增
			user.setLastUpdateTime(now);
			user.setCreateTime(now);
			this.mapper.insert(user);
		} else {
			// 更新
			cert.setUserName(user.getUserName());
			cert.setLastUpdateTime(now);
			this.mapper.updateById(cert);
		}
	}

	@Override
	public boolean deleteById(String id) {
		return this.mapper.deleteById(id) > 0;
	}

}
